﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

#region Soil
namespace backoffice.Data
{
    partial class Soil
    {
        public void setSoil(Soil s)
        {
            this.PlantID = s.PlantID;
            this.pHMin = s.pHMin;
            this.pHMax = s.pHMax;
            this.PhosphorusMin = s.PhosphorusMin;
            this.PhosphorusOpt = s.PhosphorusOpt;
            this.PhosphorusMax = s.PhosphorusMax;
            this.PotassiumMin = s.PotassiumMin;
            this.PotassiumOpt = s.PotassiumOpt;
            this.PotassiumMax = s.PotassiumMax;
            this.NitrogenMin = s.NitrogenMin;
            this.NitrogenOpt = s.NitrogenOpt;
            this.NitrogenMax = s.NitrogenMax;
        }
    }
}
#endregion

#region dataSoil
namespace backoffice.Data
{
    public class dataSoil
    {
        #region Create
        public static void createSoil(Soil s)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                db.Soils.InsertOnSubmit(s);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Get
        public static Soil getSoil(Guid guid)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                Soil s = db.Soils.FirstOrDefault(soil => soil.PlantID == guid);

                return s;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Soil> getSoils(Expression<Func<Soil, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Soils.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Set
        public static Boolean changeSoil(Soil s)
        {
            try
            {
                if (s == null) return false;
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    Soil res = db.Soils.FirstOrDefault(soil => soil.PlantID == s.PlantID);

                    if (res == null) return false;
                    else
                    {
                        res.setSoil(s);

                        db.SubmitChanges();

                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteSoil(Soil s)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                if (db.Soils.Contains(s))
                {
                    Soil res = db.Soils.FirstOrDefault(soil => soil.PlantID == s.PlantID);

                    if (res == null)
                    {
                        return false;
                    }
                    else
                    {
                        db.Soils.DeleteOnSubmit(res);
                        
                        db.SubmitChanges();

                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}
#endregion